package site.zhouinfo.ScreenMonitor;

import java.awt.*;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.zip.ZipInputStream;
import javax.imageio.ImageIO;
import javax.swing.*;

public class ShellClient extends JFrame {
    Dimension screenSize;

    public ShellClient() {
        super();
        screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        this.setSize(800, 640);//设置Frame初始
        Screen p = new Screen();//屏幕类
        Container c = this.getContentPane();
        c.setLayout(new BorderLayout());//布局
        c.add(p, "Center");//添加一个Panel
        new Thread(p).start();//开启线程
        this.show();//显示本Frame
    }

    public static void main(String[] args) {
        new ShellClient();
    }

    class Screen extends JPanel implements Runnable {
        private Image cimage;

        public Screen() {
            super();
            this.setLayout(null);
        }

        public void run() {
            ServerSocket ss = null;
            try {
                ss = new ServerSocket(5000);//探听5000端口的连接
                while (true) {
                    Socket s = null;
                    try {
                        s = ss.accept();//获取一个SOCKET
                        ZipInputStream zis = new ZipInputStream(s.getInputStream());
                        zis.getNextEntry();//获得ZIP流的ENTRY
                        cimage = ImageIO.read(zis);//把ZIP流转换为图片
                        //cimage = ImageIO.read(new FileInputStream("c:/1.jpg"));
                        repaint();//重画
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        if (s != null) {
                            try {
                                s.close();
                            } catch (IOException e) {
                            }
                        }
                    }
                }
            } catch (Exception e) {
            } finally {
                if (ss != null) {
                    try {
                        ss.close();
                    } catch (IOException e) {
                    }
                }
            }
        }

        public void paint(Graphics g) {
            super.paint(g);
            Graphics2D g2 = (Graphics2D) g;
            g2.drawImage(cimage, 0, 0, null);
        }
    }
}
